package com.relytosoft.mizar.analyze.structure.services.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.core.utils.CommonUtils;
import com.relytosoft.mizar.analyze.base.entity.pojo.dto.TaskRequestDto;
import com.relytosoft.mizar.analyze.base.enums.DeleteStatusEnum;
import com.relytosoft.mizar.analyze.base.enums.TaskStatusEnum;
import com.relytosoft.mizar.analyze.base.enums.TaskTypeEnum;
import com.relytosoft.mizar.analyze.structure.api.entity.AyStructureTaskSon;
import com.relytosoft.mizar.analyze.structure.api.pojo.dto.AyStructureTaskSonDto;
import com.relytosoft.mizar.analyze.structure.api.pojo.dto.AyTaskIdDto;
import com.relytosoft.mizar.analyze.structure.api.pojo.query.AyStructureTaskSonQuery;
import com.relytosoft.mizar.analyze.structure.api.pojo.vo.AyCameraVo;
import com.relytosoft.mizar.analyze.structure.services.service.AyStructureTaskService;
import com.relytosoft.mizar.analyze.structure.services.service.AyStructureTaskSonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author: zyc
 * @version: v1.0
 * @description: com.relytosoft.mizar.analyze.structure.services.controller
 * @Date: 2022/5/13 9:52
 */
@Api(tags = {"结构化分析子任务"})
@RestController
@RequestMapping("/structure/task/son")
public class AyStructureTaskSonController {

    @Resource
    private AyStructureTaskSonService ayStructureTaskSonService;

    @Resource
    private AyStructureTaskService ayStructureTaskService;


    @ApiOperation("创建结构化分析子任务")
    @PostMapping({"/insert"})
    public ResponseResult<Void> insert(@RequestBody AyStructureTaskSonDto ayStructureTaskSonDto) {
        AyStructureTaskSon ayStructureTaskSon = new AyStructureTaskSon();
        ayStructureTaskSon.setStructureTaskId(ayStructureTaskSonDto.getStructureTaskId());
        ayStructureTaskSon.setType(TaskTypeEnum.REAL_TIME.getValue());
        ayStructureTaskSon.setIsDeleted(DeleteStatusEnum.NOT_DELETED.getValue());
        ayStructureTaskSon.setStatus(TaskStatusEnum.CREATED.getValue());
        ayStructureTaskSon.setTaskName(ayStructureTaskService.getById(ayStructureTaskSonDto.getStructureTaskId()).getName());
        for (AyCameraVo camera : ayStructureTaskSonDto.getAyCameraVos()) {
            ayStructureTaskSon.setCameraId(camera.getCameraId());
            ayStructureTaskSon.setCameraName(camera.getCameraName());
            ayStructureTaskSon.setVideoUrl(camera.getVideoUrl());
            ayStructureTaskSon.setId(null);
            TaskRequestDto requestDto = ayStructureTaskSonService.createRequest(ayStructureTaskSon);
            boolean result = ayStructureTaskSonService.insertAyStructureTaskSon(ayStructureTaskSon);
            if (result) {
                ayStructureTaskSonService.insertStructureTaskSonKafkaOpMsg(requestDto);
            }
        }
        return ResponseResult.ok("创建结构化子任务成功");
    }


    @ApiOperation("删除结构化分析子任务")
    @PutMapping({"/delete"})
    public ResponseResult<Void> delete(@RequestBody AyTaskIdDto ayTaskIdDto) {
        boolean result = ayStructureTaskSonService.updateAyStructureTaskSon(ayTaskIdDto);
        if (result) {
            return ResponseResult.ok("删除结构化分析子任务成功");
        }
        return ResponseResult.fail("删除结构化分析子任务失败");
    }


    @ApiOperation("启动结构化分析子任务")
    @PostMapping({"/start"})
    public ResponseResult<Void> start(@RequestBody AyTaskIdDto ayTaskIdDto) {
        List<AyStructureTaskSon> ayStructureTaskSons = ayStructureTaskSonService.listByIds(ayTaskIdDto.getIds());
        for (AyStructureTaskSon taskSon : ayStructureTaskSons) {
            boolean result = ayStructureTaskSonService.startAyStructureTaskSon(ayTaskIdDto, taskSon);
            if (result) {
                TaskRequestDto requestDto = ayStructureTaskSonService.createRequest(taskSon);
                ayStructureTaskSonService.insertStructureTaskSonKafkaOpMsg(requestDto);
            }
        }
        return ResponseResult.ok("启动结构化分析子任务成功");

    }

    @ApiOperation("停止结构化分析子任务")
    @PostMapping({"/stop"})
    public ResponseResult<Void> stop(@RequestBody AyTaskIdDto ayTaskIdDto) {
        List<AyStructureTaskSon> ayStructureTaskSons = ayStructureTaskSonService.listByIds(ayTaskIdDto.getIds());
        Integer structureTaskId = ayStructureTaskSonService.getStructureTaskId(ayStructureTaskSons);
        for (AyStructureTaskSon taskSon : ayStructureTaskSons) {
            boolean result = ayStructureTaskSonService.stopAyStructureTaskSon(ayTaskIdDto, taskSon);
            if (result) {
                ayStructureTaskSonService.deleteStructureTaskSonKafkaOpMsg(taskSon.getTaskId());
            }
        }
        ayStructureTaskSonService.updateTaskStatus(structureTaskId);
        return ResponseResult.ok("停止结构化分析子任务成功");
    }

    @ApiOperation("查询结构化分析子任务列表")
    @GetMapping({"/list"})
    public ResponseResult<Map<String, Object>> select(@RequestParam("queryBodys") String bodys, @RequestParam("pageQuery") String query) {
        AyStructureTaskSonQuery ayStructureTaskSonQuery = JSON.parseObject(bodys, AyStructureTaskSonQuery.class);
        PageQuery pageQuery = JSON.parseObject(query, new TypeReference<PageQuery>() {
        });
        IPage<AyStructureTaskSon> page = ayStructureTaskSonService.selectAyStructureTaskSonPageList(pageQuery, ayStructureTaskSonQuery);
        return ResponseResult.ok(CommonUtils.getPageData(page));
    }

}
